//
//  HMComposeToolbar.swift
//  WeiBo14
//
//  Created by HM on 16/3/11.
//  Copyright © 2016年 HM. All rights reserved.
//  UIStackView 是一个容器视图,不具有显示功能

import UIKit


/// 定义不同按钮的枚举
///
/// - Picture:  图片
/// - Mention:  @
/// - Trend:    话题
/// - Emoticon: 表情图标
/// - Add:      加号
enum HMComposeToolbarButtonType: Int {
    case Picture = 0, Mention, Trend, Emoticon, Add
}

class HMComposeToolbar: UIStackView {
    
    // 是否是系统键盘,默认是true
    var isSystemKeyboard: Bool = true {
        didSet{
            let button = self.viewWithTag(HMComposeToolbarButtonType.Emoticon.rawValue)! as! UIButton
            var imageNamed = "compose_keyboardbutton_background"
            if isSystemKeyboard {
                // 是系统键盘,显示笑脸
                imageNamed = "compose_emoticonbutton_background"
            }
            button.setImage(UIImage(named: imageNamed), forState: UIControlState.Normal)
            button.setImage(UIImage(named: "\(imageNamed)_highlighted"), forState: UIControlState.Highlighted)
        }
    }
    
    
    // 当按钮点击的时候调用的闭包
    var composeToolBarButtonClick: ((type: HMComposeToolbarButtonType)->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        // 容器视图是不具在显示的功能的
        backgroundColor = UIColor.redColor()
        // 设置大小相等并且填充父控件
        distribution = .FillEqually
        
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        addChildButton("compose_add_background", type: .Add)
    }
    
    
    /// 添加子按钮的方法
    ///
    /// - parameter imageNamed: <#imageNamed description#>
    private func addChildButton(imageNamed: String, type: HMComposeToolbarButtonType){
        // 初始化按钮
        let button = UIButton()
        // 设置按钮的tag 以区分是哪个按钮点击了
        button.tag = type.rawValue
        
        // 添加监听事件
        button.addTarget(self, action: "childButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        // 设置不同状态的图片
        button.setImage(UIImage(named: imageNamed), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageNamed)_highlighted"), forState: UIControlState.Highlighted)
        
        // 设置背景图
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        // UIStackView添加子控件的方式
        addArrangedSubview(button)
    }
    
    /// 按钮点击的事件触发方法
    ///
    /// - parameter button: <#button description#>
    @objc private func childButtonClick(button: UIButton){
        // 调用外界传入的闭包
        composeToolBarButtonClick?(type: HMComposeToolbarButtonType(rawValue: button.tag)!)
    }
}
